﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using WidgetInterface;

/// <summary>
/// Summary description for IndustrialGrid
/// </summary>
/// 
namespace IndustrialDashboard
{
    [Serializable]
    public class IndustrialGrid : IWidget
    {
        public int columns { get; set; }
        public string[,] matrix { get; set; }
        public List<Dictionary<string, object>> metadata { get; set; }
        public string message { get; set; }
        private string MsgLog = "";
        public IndustrialGrid()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public IndustrialGrid(DataSet ds)
        {
            this.FillIndustrialGrid(ds);
        }
        public void FillIndustrialGrid(DataSet ds)
        {
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if ((ds.Tables[0].Columns.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                    {
                        columns = ds.Tables[0].Columns.Count;
                        matrix = toStringMatrix(ds.Tables[0]);
                        if (ds.Tables.Count > 1)
                        {
                            metadata = this.GetNormalizedRows(ds.Tables[1]);
                        }
                        else
                        {
                            metadata = null;
                        }
                    }
                    else
                    {
                        throw new UserException("The data table has no tables columns or tables rows");
                    }
                }
                else
                {
                    throw new UserException("The dbo's data has no data tables");
                }
            }
            else
            {
                throw new UserException("The data set is null");
            }
        }
        public string GetMessageLog
        {
            get
            {
                return MsgLog;
            }
        }
        private string[,] toStringMatrix(DataTable dt)
        {
            try
            {
                string myPattern = "&#";
                string myReplace = "&amp#";
                Regex myRegExp = new Regex(myPattern);
                string[,] mat = new string[dt.Rows.Count + 1, dt.Columns.Count];
                int i = 0;
                int j = 0;
                foreach (DataColumn dc in dt.Columns){mat[i, j++] = dc.ColumnName.Trim();/*j++;*/}
                i++;
                foreach (DataRow r in dt.Rows){
                    j = 0;
                    foreach (object item in r.ItemArray){
                        mat[i,j] = myRegExp.Replace(item.ToString().Trim(), myReplace);j++;
                    }
                    i++;
                }
                return mat;
            }
            catch (FormatException fe)
            {
                throw new UserException("The Industrial Grid's Matrix has not the correct format. Error: " + fe.Message);
            }
            catch (Exception ex)
            {
                throw new Exception("The Industrial Grid's Matrix has not the correct format. Error: " + ex.Message);
            }
        }
        private List<Dictionary<string, object>> GetNormalizedRows(DataTable dt)
        {
            try
            {
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Array.ForEach(
                    dt.Select(),
                    row =>
                    {
                        Dictionary<string, object> Dictionary = new Dictionary<string, object>();
                        foreach (DataColumn col in dt.Columns)
                            Dictionary.Add(col.ColumnName, row[col]);
                        rows.Add(Dictionary);
                    }
                );
                return rows;
            }
            catch (FormatException fe)
            {
                throw new UserException("Problems getting normalized rows. Error: " + fe.Message);
            }
            catch (Exception ex)
            {
                throw new Exception("Problems getting normalized rows. Error: " + ex.Message);
            }
        }
        #region "Implements"
        public object GenerateResponse(DataSet ds)
        {
            IndustrialGrid tr = new IndustrialGrid(ds);
            return (Object)tr;
        }
        public object GetExcelChunk(System.IO.Stream document)
        {
            //throw new NotImplementedException();
            return null;
        }
        public object GetPDFChunk(System.IO.Stream document)
        {
            //throw new NotImplementedException();
            return null;
        }
        #endregion 
    }
}